package com.kevin.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.nutz.mvc.Mvcs;

public class MyFilter implements Filter{
	private Map<String,String> forwordUrlMap = new HashMap<String, String>();

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest)request;
//		String localurl = req.getServletPath();
//		System.out.println(localurl);
//		RequestPath path = Mvcs.getRequestPathObject(localurl);
//        System.out.println("==========="+path);
//        String suffix = StringUtils.substringAfterLast(localurl, ".");
//        if(localurl.equals("/admin/articleinfo/addArticleInfo1.shtml")){
//        	String forwardUrl = "/WEB-INF";
//        	String dirPath = localurl.split("/")[1];
//        	System.out.println(" "+dirPath);
//        	if(forwordUrlMap.containsKey(dirPath)){
//        		forwardUrl += localurl.replace(dirPath, forwordUrlMap.get(dirPath)).replace(suffix, "jsp");
//        		System.out.println(forwardUrl+" "+forwordUrlMap);
//        		request.getRequestDispatcher(forwardUrl).forward(request, response);
//        		return;
//        	}
//        }
//		String localizationKey = Mvcs.getLocalizationKey();
////		if(localizationKey == null){
//			Locale locale = Locale.SIMPLIFIED_CHINESE;
//			locale = req.getLocale();
//			Mvcs.setLocalizationKey(locale.toString());
//			System.out.println(Mvcs.setLocalizationKey(locale.toString()));
//			System.out.println(locale.toString());
//			System.out.println(Mvcs.getLocalizationKey());
////		}
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String[] forwardUrls=config.getInitParameter("forward").split(";");
		if(forwardUrls != null){
			for(String fu:forwardUrls){
				String[] fus = fu.split("=");
				if(fus.length == 2){
					forwordUrlMap.put(fus[0], fus[1]);
					
				}else{
					System.err.println("web.xml 中 过滤器rightAccessFilter的forword属性"+fu+"有误，请用=链接键值对！");
				}
			}
		}
	}

	

}
